<script lang="ts" setup name="XtxCity">
import { ref, onMounted, watch } from "vue";
import { onClickOutside } from "@vueuse/core";

import type { AreaList, ChangeResult } from "@/types/goods";
import axios from "axios";

const active = ref(false);
const target = ref(null);
onClickOutside(target, () => {
  // 点击了target的外边，就会调用该函数
  active.value = false;
});

// 区域列表数据
const areaList = ref<AreaList[]>([]);
const cacheList = ref<AreaList[]>([]);

// 获取区域列表
const getAreaList = async () => {
  const res = await axios.get<AreaList[]>(
    "https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json"
  );
  areaList.value = res.data;
  cacheList.value = res.data;
};

onMounted(() => {
  getAreaList();
});

// 选择城市之后的结果，这个数据将来要传递给父组件
const changeResult = ref({
  provinceCode: "",
  provinceName: "",
  cityCode: "",
  cityName: "",
  countyCode: "",
  countyName: "",
});

defineProps<{
  userAddress: string;
}>();

const emit = defineEmits<{
  (e: "selectCity", value: ChangeResult): void;
}>();

const changeSelect = (item: AreaList) => {
  console.log(item);
  // 点击了省一级
  if (item.level === 0) {
    changeResult.value.provinceCode = item.code;
    changeResult.value.provinceName = item.name;

    areaList.value = item.areaList;
  } else if (item.level === 1) {
    // 点击了市一级
    changeResult.value.cityCode = item.code;
    changeResult.value.cityName = item.name;

    areaList.value = item.areaList;
  } else if (item.level === 2) {
    // 点击了区一级
    changeResult.value.countyCode = item.code;
    changeResult.value.countyName = item.name;

    // 关闭弹层
    active.value = false;
    // 子传父
    emit("selectCity", changeResult.value);
  }
};

// 关闭弹层，重新赋值
watch(active, (value) => {
  if (!value) {
    // 关闭的时候
    areaList.value = cacheList.value;
  }
});
</script>
<template>
  <div class="xtx-city" ref="target">
    <div class="select" :class="{ active: active }" @click="active = !active">
      <span class="placeholder" v-if="!userAddress">请选择配送地址</span>
      <span class="value" v-else>{{ userAddress }}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="active">
      <span
        class="ellipsis"
        @click="changeSelect(item)"
        v-for="item in areaList"
        :key="item.code"
        >{{ item.name }}</span
      >
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
